//
//  DrawingWaitViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/3/2.
//

#import "DrawingWaitViewController.h"
#import <UIImageView+WebCache.h>
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
@interface DrawingWaitViewController ()
@property (nonatomic, strong) UIImageView * bgimage;
@property (nonatomic , strong) UIButton * deletedBtn;
@property (nonatomic , strong) UIView * jinduView;
@property (nonatomic , strong) UILabel * jinduLabel;
@property (nonatomic , strong) NSTimer * time;
@end

@implementation DrawingWaitViewController
- (void)dealloc{
    if (self.time) {
        [self.time invalidate];
        self.time = nil;
    }
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)SQ_UIViewForSelfView{
    
    UIImageView * topImage = [[UIImageView alloc]init];
    topImage.image = SQGetImage(@"图片生成中");
    [self.view addSubview:topImage];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(122);
            make.top.equalTo(self.view).offset(TopHeight+20);
    }];
    UILabel * label1 = [[UILabel alloc]init];
    label1.text = [NSString stringWithFormat:@"%@%@",SQNSLocalString(@"图片加载生成中"),SQNSLocalString(@"请耐心等待")];
    label1.textColor = SQColor(@"#4AF8CE");
    label1.font = FONT_SEMIBOLD(20);
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(topImage.mas_bottom).offset(16);
    }];
    label1.keywords = SQNSLocalString(@"请耐心等待");
    label1.keywordsColor = SQColor333333;
    label1.keywordsFont = FONT_SEMIBOLD(16);
    [label1 getLableSizeWithMaxWidth:200];
    
    UILabel * label2 = [[UILabel alloc]init];
    label2.text =  SQNSLocalString(@"根据图片复杂程度，平均等待30s-2分钟");
    label2.textColor = SQColor333333;
    label2.font = FONT_REGULAR(14);
    label2.backgroundColor = SQColor(@"#4AF8CE");
    label2.layer.cornerRadius = 15;
    label2.layer.masksToBounds = YES;
    label2.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(topImage.mas_bottom).offset(50);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(310);
    }];
    
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = SQColor333333;
    bgView.layer.cornerRadius = 6;
    bgView.layer.masksToBounds = YES;
    bgView.clipsToBounds = YES;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(320);
            make.height.mas_equalTo(12);
            make.centerX.equalTo(self.view);
            make.top.equalTo(topImage.mas_bottom).offset(210);
    }];
    [bgView addSubview:self.jinduView];
    [self.jinduView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(bgView);
        make.width.mas_equalTo(1);
    }];
    [self.view addSubview:self.jinduLabel];
    [self.jinduLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView.mas_bottom).offset(12);
            make.left.equalTo(self.jinduView.mas_right).offset(-20);
    }];
    
    [self.view addSubview:self.bgimage];
    [self.bgimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
    [self.view addSubview:self.deletedBtn];
    [self.deletedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-50);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-40);
    }];
  
    if ([self.model.url isNotEmpty]) {
        self.zx_backBtnImageName = @"返回键-01";
        self.bgimage.hidden = NO;
        self.deletedBtn.hidden = NO;
        [self.bgimage sd_setImageWithURL:SQURLImage(self.model.url)];
    }else{
        self.bgimage.hidden = YES;
        self.deletedBtn.hidden = YES;
        [[NSRunLoop currentRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];
         
    }
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.model.userid forKey:@"id"];
    [HttpTool post:@"img/progress" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model = [MyChartingModel mj_objectWithKeyValues:httpModel.data];
        [self SQ_UIReloade];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
-(void)SQ_UIReloade{
    self.jinduLabel.text = [NSString stringWithFormat:@"%.f%@",self.model.progress,@"%"];
    [UIView animateWithDuration:0.5 animations:^{
        [self.jinduView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(320*(self.model.progress/100));
        }];
    }];
    
    if ([self.model.url isNotEmpty]) {
         [self.bgimage sd_setImageWithURL:SQURLImage(self.model.url) placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
             self.zx_backBtnImageName = @"返回键-01";
                self.bgimage.hidden = NO;
                self.deletedBtn.hidden = NO;
             
        }];
        if (self.time) {
            [self.time invalidate]; //停止计时器
            self.time = nil;
         }
        self.jinduLabel.text  = @"99%";
    }
}

-(void)SQ_ButtonClick{
    [self.bgimage sd_setImageWithURL:SQURLImage(self.model.url) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }];
}
#pragma mark -- 保存图片结果
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
//    [YJProgressHUD showMessage:@"下载成功" inView:self.bgimage];
    if (!error) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"已下载,请在相册中查看")];
    } else {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"保存失败,请重试!")];
    }
}
 

- (UIImageView *)bgimage{
    if (!_bgimage) {
        _bgimage = [[UIImageView alloc]init];
        _bgimage.contentMode = UIViewContentModeScaleAspectFit;
        _bgimage.clipsToBounds = YES;
        _bgimage.backgroundColor = UIColor.blackColor;
    }
    return _bgimage;
}
- (UIButton *)deletedBtn{
    if (!_deletedBtn) {
        _deletedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deletedBtn.hidden = YES;
        _deletedBtn.backgroundColor = SQColor(@"#4AF8CE");
        [_deletedBtn setTitle:SQNSLocalString(@"下载图片") forState:normal];
        [_deletedBtn setTitleColor:SQColor333333 forState:normal];
        _deletedBtn.layer.cornerRadius = 6;
        _deletedBtn.layer.masksToBounds = YES;
        [_deletedBtn addTarget:self action:@selector(SQ_ButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deletedBtn;
}

- (UIView *)jinduView{
    if (!_jinduView) {
        _jinduView = [[UIView alloc]init];
        _jinduView.backgroundColor = SQColor(@"#4AF8CE");
    }
    return _jinduView;
}
- (UILabel *)jinduLabel{
    if (!_jinduLabel) {
        _jinduLabel  = [[UILabel alloc]init];
        _jinduLabel.textColor = SQColor(@"#4AF8CE");
        _jinduLabel.font = FONT_SEMIBOLD(16);
        _jinduLabel.text = @"1%";
    }
    return _jinduLabel;
}
- (NSTimer *)time{
    if (!_time) {
        _time = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(SQ_Header) userInfo:nil repeats:YES];
    }
    return _time;
}
 
@end
